import 'package:flutter/material.dart';
import 'package:base/widget/animation/loading/loading_painter.dart';

class AnimaLoading extends StatefulWidget {
  @override
  _AnimaLoadingState createState() => _AnimaLoadingState();
}

class _AnimaLoadingState extends State<AnimaLoading>
    with TickerProviderStateMixin {
  AnimationController outerController, innerController;
  Animation outerAnima, innerAnima;

  @override
  void dispose() {
    outerController?.dispose();
    innerController?.dispose();
    super.dispose();
  }

  @override
  void initState() {
    super.initState();
    outerController = AnimationController(
      vsync: this,
      duration: Duration(milliseconds: 3000),
    );
    innerController = AnimationController(
      vsync: this,
      duration: Duration(milliseconds: 2000),
    );

    outerAnima = Tween(begin: 0.0, end: 2.0).animate(outerController);
    innerAnima = Tween(begin: 1.0, end: 0.0).animate(innerController);

    outerController.addStatusListener((status) {
      controllerFn(outerController, status);
    });
    innerController.addStatusListener((status) {
      controllerFn(innerController, status);
    });
  }

  void controllerFn(AnimationController controller, AnimationStatus status) {
    switch (status) {
      case AnimationStatus.completed:
        controller.reset();
        controller.forward();
        break;
      case AnimationStatus.dismissed:
        controller.forward();
        break;
      default:
        print(status);
    }
  }

  @override
  Widget build(BuildContext context) {
    if (!outerController.isAnimating) outerController.forward();
    if (!innerController.isAnimating) innerController.forward();
    return Container(
      color: Colors.white,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Stack(
            alignment: Alignment.center,
            children: <Widget>[
              RotationTransition(
                turns: outerAnima,
                child: Container(
                  width: 100,
                  height: 100,
                  child: CustomPaint(
                    painter: OuterPainter(),
                  ),
                ),
              ),
              RotationTransition(
                turns: innerAnima,
                child: Container(
                  width: 86,
                  height: 86,
                  child: CustomPaint(
                    painter: InnerPainter(),
                  ),
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }
}
